package cn.codergege.demo.controller

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import javax.servlet.http.HttpSession

@RequestMapping("/user")
@Controller
class UserController {

    @PostMapping("/login")
    fun login(@RequestParam("username") username: String,
              @RequestParam("password") password: String,
              map: MutableMap<String, Any>,
              session: HttpSession): String {
        if(username.isBlank() or username.isEmpty() or (password != "111111")) {
            // 登录失败
            map["msg"] = "用户名密码错误!"
            return "login"
        } else {
            // 放入session
            session.setAttribute("loginUsername", username)
            // 登录成功, 防止表单重复提交, 可以重定向到 emp/list.html
            return "redirect:/emp/main.html"
        }
    }
}